package center.wxp.mapper.controller;

import center.wxp.mapper.model.dto.UserListAnnotationDto;
import center.wxp.mapper.model.dto.UserListDto;
import center.wxp.mapper.model.vo.UserListVo;
import center.wxp.mapper.support.utils.Result;
import center.wxp.mapper.model.entity.User;
import center.wxp.mapper.service.UserService;
import center.wxp.mapper.support.utils.enums.StatusCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.template.annotation.ConditionRewrite;

/**
 * @author wuxiaopeng
 * @date 2020/03/23
 */
@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "UserController")
public class UserController {
    @Resource
    private UserService userService;

    @ApiOperation(value = "Annotation查询参数实体类（list不分页）")
    @GetMapping("/objAnnotationList")
    public Result<List<User>> objAnnotationList(UserListAnnotationDto dto) {
        List<User> list = new ArrayList<>();
        try {
            Condition condition = ConditionRewrite.equalToCondition(new Condition(User.class), dto);
            list = userService.findListByCondition(condition);
        } catch (Exception e) {
            log.error("UserController 获取信息异常:{}", e);
            return Result.failure().setCode(StatusCode.FAILURE.getCode()).setMsg("UserController 获取信息异常!");
        }
        return Result.ok().setData(list);
    }

    @ApiOperation(value = "Obj查询参数实体类（list不分页）")
    @GetMapping("/objList")
    public Result<List<UserListVo>> objList(UserListDto dto) {
        List<User> list = userService.findListByObject(dto);
        return Result.ok().setData(list);
    }

    @ApiOperation(value = "Map查询参数实体类（list不分页）")
    @GetMapping("/mapList")
    public Result<List<User>> mapList(UserListDto dto) {
        List<User> list = new ArrayList<>();
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("id", dto.getId());
            map.put("name", dto.getName());
            list = userService.findListByMap(map);
        } catch (Exception e) {
            log.error("UserController 获取信息异常:{}", e);
            return Result.failure().setCode(StatusCode.FAILURE.getCode()).setMsg("UserController 获取信息异常!");
        }
        return Result.ok().setData(list);
    }

    @ApiOperation(value = "单个属性查询（list不分页）")
    @GetMapping("/single List")
    public Result<List<User>> singleList(Long id) {
        List<User> list = new ArrayList<>();
        try {
            list = userService.findListByOnly("id", id);
        } catch (Exception e) {
            log.error("UserController 获取信息异常:{}", e);
            return Result.failure().setCode(StatusCode.FAILURE.getCode()).setMsg("UserController 获取信息异常!");
        }
        return Result.ok().setData(list);
    }

    @ApiOperation(value = "获取信息（list分页）")
    @GetMapping("/listPages")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNum", value = "起始页"),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "页大小")
    })
    public Result<List<User>> listPages(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        List<User> list = new ArrayList<>();
        try {
            PageHelper.startPage(pageNum, pageSize);
            list = userService.findListAll();
        } catch (Exception e) {
            log.error("UserController 获取信息异常:{}", e);
            return Result.failure().setCode(StatusCode.FAILURE.getCode()).setMsg("UserController 获取信息异常!");
        }
        PageInfo pageInfo = new PageInfo(list);
        return Result.ok().setData(pageInfo);
    }

    @ApiOperation(value = "添加信息")
    @PostMapping("/add")
    public Result add(User user) {
        String id = userService.saveSelectiveId(user);
        return Result.ok().setData(id);
    }

    @ApiOperation(value = "添加或更新信息")
    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(User user) {
        int count = userService.saveOrUpdateKeySelective(user);
        return Result.ok().setData(count);
    }

    @ApiOperation(value = "批量保存或更新信息")
    @PostMapping("/batchAddOrUpdate")
    public Result batchAddOrUpdate(@RequestBody List<User> list) {
        for (User user : list) {
            user.setCreateDate(new Date());
        }
        int count = userService.saveOrUpdateKeyList(list);
        return Result.ok().setData(count);
    }

    @ApiOperation(value = "修改信息")
    @PutMapping("/update")
    public Result update(User user) {
        try {
            userService.updateBySelectiveObject(user, user);
        } catch (Exception e) {
            log.error("UserController 更新信息异常:{}", e);
            return Result.failure().setCode(StatusCode.FAILURE.getCode()).setMsg("UserController 更新信息异常!");
        }
        return Result.ok();
    }

    @ApiOperation(value = "删除信息（主键）")
    @DeleteMapping("/delete")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "ids", value = "ids",allowMultiple = true)
    })
    public Result delete(@RequestParam String[] ids) {
        try {
            Condition condition = new Condition(User.class);
            Condition.Criteria criteria = condition.createCriteria();
            criteria.andNotIn("id", Arrays.asList((String[]) ids));
            criteria.andEqualTo("age", 12);
            userService.deleteBySelectCondition(condition);
        } catch (Exception e) {
            log.error("UserController 更新信息异常:{}", e);
            return Result.failure().setCode(StatusCode.FAILURE.getCode()).setMsg("UserController 更新信息异常!");
        }
        return Result.ok();
    }
}
